//
//  UIView+UiViewSubView.m
//  QianCheng
//
//  Created by mac on 16/3/11.
//  Copyright © 2016年 静静. All rights reserved.
// 一些控件的创建待完善

#import "UIView+UiViewSubView.h"

@implementation UIView (UiViewSubView)

/** 创建nav图片btn **/
+ (UIButton *)buttonWithFrame:(CGRect)frame ImageName:(NSString*)imageName BackImageName:(NSString*)backImageName Target:(id)target Action:(SEL)action Title:(NSString*)title
{
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/** 创建nav文字btn **/
+ (UIButton*)buttonWithFrame:(CGRect)frame Title:(NSString*)title TitleColor:(UIColor*)color TitleFont:(CGFloat)font Target:(id)target Action:(SEL)action
{
    UIButton*button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:font];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/** 只有图片，选中与默认切换 **/
+(UIButton *)buttonwithFrame:(CGRect)frame setImageName:(NSString *)imageName setSelImageName:(NSString *)disImageName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    button.frame = frame;
    [button setImage:[UIImage imageNamed: imageName]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed: disImageName]forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/** 只有图片，disabel与默认切换*/
+(UIButton *)buttonwithFrame:(CGRect)frame setImageName:(NSString *)imageName setDisImageName:(NSString *)disImageName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    button.frame = frame;
    [button setImage:[UIImage imageNamed: imageName]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed: disImageName]forState:UIControlStateDisabled];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/** imageView的创建 **/
+(UIImageView *)imageViewWithFrame:(CGRect)frame setImageName:(NSString *)imageName;
{
    //创建底部的线
    UIImageView *imageView = [[UIImageView alloc]  init];
    imageView.frame = frame;
    imageView.image =[UIImage imageNamed:imageName];
    return imageView;
}

/** label的创建 **/
+(UILabel *)labelWithFrame:(CGRect)frame text:(NSString *)text fontSize:(NSInteger )fontsize textColor:(UIColor *)textColor
{
    UILabel *label = [[UILabel alloc] init];
    label.frame = frame;
    label.text = [NSString stringWithFormat:@"%@",text];
    label.font = [UIFont systemFontOfSize:fontsize];
    label.textColor = textColor;
    return label;
}

/** TextField的创建 **/
+(UITextField *)textFileWithFrame:(CGRect)frame text:(NSString *)text fontSize:(NSInteger )fontsize textColor:(UIColor *)textColor{
    UITextField *textFiled = [[UITextField alloc] initWithFrame:frame];
    textFiled.textColor = textColor;
    textFiled.font = [UIFont systemFontOfSize:fontsize];
    return textFiled;
}

/** 创建searchBar **/
+ (UISearchBar *)createSearchBarWithFrame:(CGRect)frame withBgImage:(NSString *)image withPlaceholder:(NSString *)placeholder;
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:frame];
    
    //去掉原有背景色
    [[[[ searchBar.subviews objectAtIndex:0]subviews]objectAtIndex:0]removeFromSuperview ];
    searchBar.backgroundColor=[UIColor clearColor];
    
    //使用图片修改背景色
    //    if (image) {
    //        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
    //        imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    //        [searchBar insertSubview:imageView atIndex:1];
    //    }
    
    //    //经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象
    searchBar.barTintColor = [UIColor whiteColor];
    UIView *searchTextField = [[[searchBar.subviews firstObject] subviews] lastObject];
    //修改里卖弄TextField的背景色
    //searchTextField.backgroundColor = [UIColor redColor];
    //searchTextField加边框
    searchTextField.layer.borderColor = [UIColor colorWithHexString:@"bbbbbb" alpha:1].CGColor;
    searchTextField.layer.borderWidth = 1;
    searchTextField.layer.cornerRadius = 5;
    
    //设置默认提示语
    searchBar.placeholder = placeholder;
    //设置光标颜色
    [searchBar setTintColor:[UIColor blueColor]];
    return searchBar;
}


//*************创建不带frame,不带文字的控件，动态cell专用*********************//

/**btn的创建（不带文字，不带背景图设置)*/
+(UIButton *)buttonwithImageName:(NSString *)imageName setDisImageName:(NSString *)disImageName target:(id)target action:(SEL)action

{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed: imageName]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed: disImageName]forState:UIControlStateDisabled];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**btn的创建（带文字,带图片,不带背景图设置)*/
+(UIButton *)buttonwithTitle:(NSString *)title titleCol:(UIColor *)titleCol setImageName:(NSString *)imageName setDisImageName:(NSString *)disImageName target:(id)target action:(SEL)action

{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed: imageName]forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed: disImageName]forState:UIControlStateDisabled];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleCol forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**btn的创建（带文字,带背景图,不带图片设置)*/
+(UIButton *)buttonwithTitle:(NSString *)title titleCol:(UIColor *)titleCol setBackImageName:(NSString *)imageName setDisImageName:(NSString *)disImageName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    [button setBackgroundImage:[UIImage imageNamed: imageName]forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed: disImageName]forState:UIControlStateDisabled];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleCol forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**imageView的创建*/
+ (UIImageView *)imageViewWithImageName:(NSString *)imageName
{
    //创建底部的线
    UIImageView *imageView = [[UIImageView alloc]  init];
    imageView.image =[UIImage imageNamed:imageName];
    return imageView;
}

/**label的创建*/
+(UILabel *)labelWithFontSize:(NSInteger )fontsize textColor:(UIColor *)textColor alignment:(NSTextAlignment)alignment
{
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:fontsize];
    label.textAlignment = alignment;
    label.textColor = textColor;
    return label;
}

/**TextField的创建*/
+(UITextField *)textFileWithFontSize:(NSInteger )fontsize textColor:(UIColor *)textColor alignment:(NSTextAlignment)alignment;
{
    UITextField *textFiled = [[UITextField alloc] init];
    textFiled.textColor = textColor;
    textFiled.textAlignment = alignment;
    textFiled.font = [UIFont systemFontOfSize:fontsize];
    return textFiled;
}

/**TextField的创建*/
+(UITextField *)textFileWithFrame:(CGRect)frame fontSize:(NSInteger )fontsize alignment:(NSTextAlignment)alignment textColor:(UIColor *)textColor placeholder:(NSString *)placeholder placeholderColor:(UIColor *)placeholderColor
{
    UITextField *searchTextField = [[UITextField alloc] initWithFrame:frame];
    if (fontsize) {
        searchTextField.font = [UIFont systemFontOfSize:fontsize];
    }
    if (alignment) {
        searchTextField.textAlignment = alignment;
    }
    if (textColor) {
        searchTextField.textColor = textColor;
    }
    if (placeholder) {
        searchTextField.placeholder = placeholder;
    }
    if (placeholderColor) {
        [searchTextField setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
    }
    return searchTextField;
}


@end
